<template>
	<view class="mall-index">
		<view class="head">
			<view class="city">
				<view class="name" @click="handleToCity">
					{{cityName}}
				</view>
				<image class="img" src="@/static/images/down.png" mode="widthFix"></image>
			</view>
			<view class="search">
				<image class="icon" src="@/static/images/search-white.png" mode="widthFixw"></image>
				<input class="content" type="text" placeholder="发现店铺更多商品,搜一搜" v-model="params.goods_name"
					placeholder-style="color:white" confirm-type="search" @confirm="handleSearch">
			</view>
		</view>
		<view class="banner">
			<image class="img" src="@/static/images/shopMall-index.jpg" mode="widthFix"></image>
		</view>
		<view class="sort">
			<view class="item default" :class="{active:activeIndex === 1}" @click="handleClick(1)">综合排序</view>
			<view class="item price" :class="{active:activeIndex === 2}" @click="handleClick(2)">
				<view>价格</view>
				<view class="price-img">
					<image v-if="activeIndex === 2 && isTop" class="img" src="@/static/images/down2-hover.png"></image>
					<image v-else class="img" src="@/static/images/down2.png"></image>

					<image v-if="activeIndex === 2 && !isTop" class="img" src="@/static/images/down1-hover.png"></image>
					<image v-else class="img" src="@/static/images/down1.png"></image>
				</view>
			</view>
			<view class="item new" :class="{active:activeIndex === 4}" @click="handleClick(4)">新品</view>
			<view class="item icon">
				<view class="line"></view>
				<image class="img" v-if="false" src="@/static/images/sort1.png" mode="widthFix"></image>
				<image class="img" v-else src="@/static/images/sort2.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="waterfall">
			<view class="item" v-for="item in itemList" :key="item.id" @click="handleGoodsDetail(item.id)">
				<image v-if="item.new_grade == 95" class="new" src="@/static/index/95.png"></image>
				<image v-else-if="item.new_grade == 99" class="new" src="@/static/index/99.png"></image>
				<image v-else class="new" src="@/static/index/new.png"></image>
				<image class="img" :src="getImg(item.imgurl)" :lazy-load="true" mode="widthFix"></image>
				<view class="content">
					<view class="title">{{item.title}}</view>
					<view class="tag-list">
						<view class="tag" v-for="(ele,index) in getTagList(item.rent_label)" :key="index"
							:class="ele.class">
							{{ele.name}}
						</view>
					</view>
					<view class="price">￥{{item.min_price}}</view>
				</view>
			</view>
		</view>
		<view class="navigation-bar">
			<view class="item" v-for="item in navList" :key="item.id" @click="handleNavClick(item.id)">
				<image class="img" :src="item.icon" mode="widthFix"></image>
				<view class="title">{{item.title}}</view>
			</view>
		</view>
		<view class="back-top" @click="handleBackTop" v-if="backTop">
			<image class="img" src="@/static/images/top.png" mode="widthFix"></image>
		</view>
	</view>
</template>
<script>
	import goodsCard from "@/components/goodsCard.vue";
	export default {
		name: "mallIndex",
		components: {
			goodsCard,
		},
		data() {
			return {
				cityName: "全国",
				activeIndex: 1,
				isTop: false,
				itemList: [],
				noMore: false,
				isLoading: false,
				params: {
					city_name: this.cityName,
					goods_name: "",
					member_id: "",
					page: 1,
					pageSize: 8,
					sort_data: 1,
				},
				navIndex: 0,
				navList: [{
					id: 1,
					title: "首页",
					icon: require("@/static/images/mallIndex/shop-icon-index-hover.png")
				}, {
					id: 2,
					title: "分类",
					icon: require("@/static/images/mallIndex/shop-icon-classify.png")
				}, {
					id: 3,
					title: "客服",
					icon: require("@/static/images/mallIndex/shop-icon-service.png")
				}, {
					id: 4,
					title: "我的",
					icon: require("@/static/images/mallIndex/shop-icon-mine.png")
				}],
				backTop: false,
			}
		},
		watch: {
			activeIndex(val) {
				this.resetData();
				this.params.sort_data = val;
				this.getGoodsList();
			},
			isTop(bool) {
				if (!bool) {
					this.resetData();
					this.params.sort_data = 3;
					this.getGoodsList();
				}
			}
		},
		onLoad(options) {
			this.params.member_id = options.member_id;
			this.getCityName();
			this.getGoodsList();
		},
		methods: {
			handleSearch() {
				const goods_name = this.params.goods_name;
				this.resetData();
				this.params.goods_name = goods_name;
				this.getGoodsList();
			},
			handleNavClick(id) {
				switch (id) {
					case 1:
						console.log("首页")
						break;
					case 2:
						console.log("分类")
						break;
					case 3:
						this.handleSerive();
						break;
					case 4:
						this.handleMine();
						break;
				}
			},
			handleSerive() {
				uni.switchTab({
					url: "/pages/service/service",
				})
			},
			handleMine() {
				uni.switchTab({
					url: "/pages/my/my",
				})
			},
			handleClick(index) {
				this.activeIndex = index;
				if (index === 2) this.isTop = !this.isTop;
			},
			resetData() {
				this.itemList.splice(0);
				this.isLoading = false;
				this.noMore = false;
				this.params.page = 1;
			},
			handleGoodsDetail(id) {
				uni.navigateTo({
					url: `/pagesA/category/goods/productDetails/productDetails?id=${id}`
				})
			},
			getTagList(label) {
				const array = label.split(",");
				const newArr = [];
				array.forEach(item => {
					const obj = {
						name: item,
						class: "",
					}
					if (item !== "包邮") {
						obj.class = "red";
					}
					newArr.push(obj);
				})
				return newArr;
			},
			getImg(url) {
				return getApp().globalData.baseImg + url;
			},
			handleToCity() {
				uni.navigateTo({
					url: '/pagesB/index/city'
				})
			},
			getCityName() {
				this.cityName = uni.getStorageSync("city") || "全国"
			},
			getGoodsList() {
				if (this.noMore || this.isLoading) return;
				this.isLoading = true;
				uni.showLoading({
					title: "Loading",
					mask: true,
				})
				uni.$http.post('/Wechat/Shop/getShopIndex', this.params)
					.then(res => {
						const data = res.data.data.data;
						if (data.length < this.params.pageSize) {
							this.noMore = true;
						} else {
							this.params.page++;
						}
						this.itemList.push(...data);
					}).finally(() => {
						this.isLoading = false;
						uni.hideLoading();
					})
			},
			handleBackTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			}
		},
		onReachBottom() {
			this.params.page++;
			this.getGoodsList();
		},
		onPageScroll(event) {
			const scrollTop = event.scrollTop;
			this.backTop = scrollTop > 800;
		}
	}
</script>
<style lang="scss" scoped>
	.mall-index {
		padding-bottom: 158rpx;

		.head {
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			margin-top: 30rpx;

			.city {
				display: flex;
				align-items: center;
				margin-right: 50rpx;

				.name {
					white-space: nowrap;
					max-width: 100rpx;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.img {
					width: 24rpx;
					height: 20rpx;
					margin-left: 10rpx;
				}
			}

			.search {
				padding: 0 20rpx;
				display: flex;
				width: 520rpx;
				height: 66rpx;
				border-radius: 30rpx;
				background-color: #adadad;
				align-items: center;
				box-sizing: border-box;

				.icon {
					width: 55rpx;
					height: 50rpx;
				}

				.content {
					width: 500rpx;
					color: white;
					margin-right: 20rpx;
				}
			}

		}

		.banner {
			margin-top: 20rpx;
			padding: 0 20rpx;

			.img {
				width: 710rpx;
				border-radius: 20rpx;
				overflow: hidden;
			}
		}

		.sort {
			margin-top: 10rpx;
			display: flex;
			padding: 5px 40rpx;
			background-color: white;
			height: 80rpx;
			box-sizing: border-box;

			.item {
				width: 177.5rpx;
				font-size: 28rpx;
				color: #c3c3c3;

				&.default {
					line-height: 60rpx;
				}

				&.price,
				&.new {
					display: flex;
					justify-content: center;
					align-items: center;
				}

				&.active {
					color: #e34339;
				}

				&.price {
					display: flex;

					.price-img {
						display: flex;
						justify-content: space-around;
						align-items: center;
						flex-direction: column;
						margin-left: 10rpx;

						.img {
							width: 15rpx;
							height: 10rpx;

							&:last-of-type {
								margin-top: 5rpx;
							}
						}
					}
				}



				&.new {
					box-sizing: border-box;
					padding-left: 40rpx;
				}

				&.icon {
					display: flex;
					justify-content: flex-end;
					align-items: center;

					.line {
						width: 2rpx;
						height: 40rpx;
						margin-right: 50rpx;
						background-color: #c3c3c3;
					}

					.img {
						width: 40rpx;
						height: 40rpx;
					}
				}

			}
		}

		.waterfall {
			display: flex;
			flex-wrap: wrap;
			padding: 24rpx;
			padding-bottom: 0;

			.item {
				width: 339rpx;
				border-radius: 16rpx;
				box-sizing: border-box;
				position: relative;
				background-color: white;
				box-shadow: 0rpx 2rpx 10rpx 0rpx rgba(0, 0, 0, 0.1);
				overflow: hidden;
				margin-bottom: 24rpx;

				&:nth-of-type(odd) {
					margin-right: 24rpx;
				}

				.new {
					position: absolute;
					top: 0;
					left: -5rpx;
					width: 84.85rpx;
					height: 86.37rpx;
					z-index: 10;
				}

				.img {
					width: 310rpx;
					margin-left: 50%;
					transform: translateX(-50%);
				}

				.content {
					padding: 20rpx 24rpx;
					padding-top: 0;

					.title {
						font-size: 28rpx;
						font-weight: bold;
					}

					.tag-list {
						display: flex;
						flex-wrap: wrap;
						margin: 10rpx 0;

						.tag {
							margin-right: 15rpx;
							margin-bottom: 10rpx;
							padding: 0 5rpx;
							font-size: 24rpx;
							color: #85d0a5;
							background-color: #effaf4;

							&.red {
								color: #e07875;
								background-color: #faeded;
							}
						}
					}

					.price {
						font-size: 28rpx;
						color: #e34339;
					}
				}
			}
		}

		.navigation-bar {
			width: 750rpx;
			height: 148rpx;
			position: fixed;
			left: 0;
			bottom: 0;
			display: flex;
			background-color: white;
			border-top: 1rpx solid #c3c3c3;
			align-items: center;
			justify-content: space-between;
			padding: 0 50rpx;
			padding-bottom: 28rpx;
			box-sizing: border-box;
			z-index: 9999;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: #c3c3c3;
				font-size: 28rpx;

				&:first-of-type {
					color: #e34339;
				}

				.img {
					width: 42rpx;
				}

				.title {
					font-size: 26rpx;
					transform: scale(.8);
				}
			}
		}

		.back-top {
			position: fixed;
			right: 50rpx;
			bottom: 200rpx;

			.img {
				width: 100rpx;
				height: 100rpx;
			}
		}
	}
</style>